package yf.mall.controller;


import java.math.BigDecimal;
import java.util.List;

import com.yf.response.RRException;
import com.yf.service.SysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import com.yf.consts.DaoConst;
import yf.mall.dao.ShopDao;
import yf.mall.dao.ShopParamDao;
import yf.mall.entity.ShopEntity;
import yf.mall.entity.ShopParamEntity;
import yf.mall.entity.UserEntity;
import yf.mall.enums.ShopStatusEnum;
import yf.mall.service.*;
import com.yf.response.R;
import yf.mall.vo.ShopTypeVo;


/**
 * 店铺Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Sun May 24 13:54:38 CST 2020
 */
@RestController
@RequestMapping("/shop")
public class  ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShopParamDao shopParamDao;
    /**
     * 列表店铺
     */
    @RequestMapping("/list")
    @RequiresPermissions("shop:list")
    public R list(@RequestParam Map<String,Object> params) {
        PageHelper.startPage(DaoConst.getPage(params),DaoConst.getLIMIT(params));
        String mcode = (String)params.get("mcode");
        String userCode = (String)params.get("userCode");
        if (mcode != null && !"".equals(mcode)){
            UserEntity mCodeEntity = userService.findByCode(mcode);
            if (mCodeEntity == null){
                throw new RRException("管理员编号不存在");
            }
            params.put("mid",mCodeEntity.getId());
        }
        if (userCode != null && !"".equals(userCode)){
            UserEntity uCodeEntity = userService.findByCode(userCode);
            if (uCodeEntity == null){
                throw new RRException("用户编号不存在");
            }
            params.put("userId",uCodeEntity.getId());
        }

        List<ShopEntity> shopList = shopService.queryList(params);
        //获取用户编号和管理编号
        if (shopList != null && shopList.size() > 0){
            for (ShopEntity shopEntity : shopList) {
                UserEntity userEntity = userService.get(shopEntity.getUserId());
                shopEntity.setUserCode(userEntity.getCode());

                if (shopEntity.getMid() != null){
                    UserEntity mUser = userService.get(shopEntity.getMid());
                    shopEntity.setMcode(mUser.getCode());
                }
            }
        }
        return R.ok(new PageInfo(shopList));
    }
    @Autowired
    private MallTypeService mallTypeService;
    /**
     * 查看店铺
     */
    @SysLog("查看店铺")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("shop:info")
    public R info(@PathVariable("id") Long id) {
        ShopEntity shop = shopService.get(id);
        if (shop.getMid() != null){
            UserEntity mUser = userService.get(shop.getMid());
            shop.setMcode(mUser.getCode());
        }
        UserEntity userEntity = userService.get(shop.getUserId());
        shop.setUserCode(userEntity.getCode());
        List typesResult = mallTypeService.findByShopId(id);
        return R.ok(shop).putExtra("types",typesResult);
    }

    /**
     * 保存店铺
     */
    @SysLog("代理申请店铺")
    @RequestMapping("/dmz/saveShop")
    @Transactional(rollbackFor = Exception.class)
    public R saveShop(@RequestBody ShopTypeVo shopVo) {
        ShopEntity shop = shopService.openShop(shopVo);
        return R.ok(shop);
    }

    /**
     * 保存店铺
     */
    @SysLog("保存店铺")
    @RequestMapping("/save")
    @RequiresPermissions("shop:save")
    public R save(@RequestBody ShopTypeVo shopVo) {
        //  isUserCodeExistence(shop);
        ShopEntity shop = shopVo.getShop();
        //判断编号是否存在
        UserEntity userEntity = codeIsExist(shop);
        if (shopService.findByUserId(userEntity.getId()) != null){
            throw new RRException("该用户已开通店铺");
        }
        if (shopService.findByName(shop.getName()) != null){
            throw new RRException("该店铺名已存在");
        }
        shop.setUserId(userEntity.getId());
        shop.setStatus(ShopStatusEnum.AUDIT.getIndex());
        shopService.save(shop);
        return R.ok(shop);
    }

    /**
     * 修改店铺
     */
    @SysLog("修改店铺")
    @RequestMapping("/update")
    @RequiresPermissions("shop:update")
    public R update(@RequestBody ShopTypeVo shopTypeVo) {
        ShopEntity shop = shopService.updateShop(shopTypeVo);
        return R.ok(shop);
    }

    @Autowired
    SysRoleService sysRoleService;

    /**
     * 删除店铺
     */
    @SysLog("删除店铺")
    @RequestMapping("/delete")
    @RequiresPermissions("shop:delete")
    public R delete(Long[]ids) {

        shopService.delByShopId(ids);

        return R.ok();
    }

    /**
     * 所有店铺
     */
    @SysLog("所有店铺")
    @RequestMapping("/queryAll")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<ShopEntity> shopList = shopService.queryList(params);

        return R.ok(shopList);
    }

    /**
     @ModelAttribute
     public SearchParam<ShopEntity> searchParam(SearchParam<ShopEntity> params, ShopEntity shop) {
     if (shop != null) {
     params.setEntity(shop);
     }

     return params;
     }
     **/
    /***********自定义开始***********/

    /**
     * 根据用户编号查看店铺
     */
    @SysLog("根据用户编号查看店铺")
    @RequestMapping("/dmz/findByUserCode/{userCode}")
    public R findByUserCode (@PathVariable("userCode") String userCode) {
        UserEntity userEntity = userService.findByCode(userCode);
        if (userEntity == null){
            throw new RRException("请输入正确的编号");
        }
        ShopEntity shopEntity = shopService.findByUserId(userEntity.getId());

        //查管理编号
        if (shopEntity != null){
            userEntity = userService.get(shopEntity.getMid());
            shopEntity.setMcode(userEntity.getCode());
        }
        return R.ok(shopEntity);
    }

    @Autowired
    MallGoodsService mallGoodsService;
    /**
     *  que
     *  撤销店铺入驻申请
     */
    @SysLog("根据用户编号和身份证号码撤销店铺入驻申请")
    @RequestMapping("/dmz/delShop")
    public R delShop( String userCode,String userCard){
        UserEntity userEntity = userService.findByCode(userCode);
        if (userEntity == null){
            throw new RRException("用户编号不存在");
        }
        ShopEntity shop = shopService.findByUserId(userEntity.getId());
        if (shop == null){
            throw new RRException("该用户尚未提交店铺入驻申请");
        }
        if (!userCard.equals(shop.getCard())){
            throw new RRException("请输入该用户绑定的正确的身份证号");
        }
        if(shop.getStatus() == ShopStatusEnum.PASS.getIndex() || shop.getStatus() == ShopStatusEnum.NOPASS.getIndex()){
            throw new RRException("该编号暂不能申请撤销,请联系客服撤销");
        }
        shopService.deleteShop(userCard,userEntity);
        return R.ok();
    }

    /**
     *  que
     *  判断用户编号和管理员是否存在
     * @param shop
     */
    private UserEntity codeIsExist(ShopEntity shop) {
        if (shop.getMcode() != null) {
            UserEntity mUser = userService.findByCode(shop.getMcode());
            if (mUser == null) {
                throw new RRException("服务人编号不存在");
            }
            shop.setMid(mUser.getId());
        }
        UserEntity userEntity = userService.findByCode(shop.getUserCode());
        if (userEntity == null) {
            throw new RRException("用户编号不存在");
        }
        if (shop.getUserCode().equals(shop.getMcode())){
            throw new RRException("服务人不能是自己");
        }
        return userEntity;
    }

    @RequestMapping("/findAll")
    public R findAll(){
        List<ShopEntity> shopEntityList = shopService.findAll();
        return R.ok(shopEntityList);
    }
}
